package yja.gzw.app.activity

import AuthResult
import PayResult
import android.annotation.SuppressLint
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import com.alipay.sdk.app.PayTask
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityHuiyuanzhongxingBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import yja.gzw.app.window.Chicun

class HuiyuanzhongxingActiviry :BaseActivity<Mainviewmodel, ActivityHuiyuanzhongxingBinding>(R.layout.activity_huiyuanzhongxing){
    var zhifu=""
  val chicun=Chicun()
    val SDK_PAY_FLAG: Int = 1

    val SDK_AUTH_FLAG: Int = 2
    override fun initView() {
        chicun.chicunwai(binding.zi3,-375,185,7,0)
        chicun.chicunwai(binding.zi4,7,185,0,0)
        chicun.chicunwai(binding.zi1,0,0,7,0)
        chicun.chicunwai(binding.zi2,7,0,0,0)

        chicun.chicun(binding.zi3,0,170)
        chicun.chicun(binding.zi4,0,170)
        chicun.chicun(binding.zi1,0,170)
        chicun.chicun(binding.zi2,0,170)
        chicun.chicunwai(binding.xuan3,-375,185,7,0)
        chicun.chicunwai(binding.xuan4,7,185,0,0)
        chicun.chicunwai(binding.xuan1,0,0,7,0)
        chicun.chicunwai(binding.xuan2,7,0,0,0)

        chicun.chicun(binding.xuan3,0,170)
        chicun.chicun(binding.xuan4,0,170)
        chicun.chicun(binding.xuan1,0,170)
        chicun.chicun(binding.xuan2,0,170)





        binding.fanhui.setOnClickListener {
            finish()

        }

        viewmodel.onwodezhuyewang(UserData.token,mapOf("id" to ""))


        binding.kaitonganniu.setOnClickListener {
            var idss="1"

            if(binding.xuan1.isChecked){
                idss="1"

            }else if(binding.xuan2.isChecked){
                idss="2"
            }else if(binding.xuan3.isChecked){
                idss="3"
            } else{

                idss="4"
            }

            if(binding.zhifu1.isChecked){
                zhifu="alipay"
            }else{
                zhifu="wechat"
            }

            viewmodel.onchuanjiandingdanvipwang(UserData.token,mapOf("vip_id" to idss))


        }



    }

    override fun dataObservese() {
        viewmodel.onZhifushow.observe(this, Observer {
            if(it.code==1){
                zhifujieu(it.data.toString())


            }else{

                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })

        viewmodel.onchuanjiandingdanvip.observe(this, Observer {
            if(it.code==1){

               // Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
                viewmodel.onZhifushowwang(UserData.token, mapOf("order_id" to it.data.id,"type" to "vip","pay_type" to zhifu,"method" to "app"))

            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })



        viewmodel.onwodezhuye.observe(this, Observer {
            if(it.code==1){
                binding.yunhuaminchheg.text=it.data.nickname
                if(it.data.avatar==""){
                    binding.touxiang.setImageResource(R.mipmap.touxiang)
                }else{
                    val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
                    Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.avatar)
                        .apply(requestOptions)
                        .into(binding.touxiang)
                }

                binding.yonghuid.text="ID: "+it.data.id.toString()
            }


        })



    }
    fun zhifujieu(info: String) {
        val orderInfo = info // 订单信息
        val payRunnable = Runnable {
            val alipay = PayTask(this)
            val result = alipay.payV2(orderInfo, true)
            val msg = Message()
            msg.what = SDK_PAY_FLAG
            msg.obj = result
            mHandler.sendMessage(msg)
        }

        val payThread = Thread(payRunnable)
        payThread.start()
    }
    @SuppressLint("HandlerLeak")
    var mHandler: Handler = object : Handler() {
        @Suppress("unused")
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                SDK_PAY_FLAG -> {
                    Log.e("支付步骤","1")
                    val payResult: PayResult = PayResult(msg.obj as Map<String?, String?>)

                    /**
                     * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    val resultInfo: String = payResult.getResult() // 同步返回需要验证的信息
                    val resultStatus: String = payResult.getResultStatus()
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        Log.e("支付步骤","2")
                        //todo 支付成功
                        Toast.makeText(this@HuiyuanzhongxingActiviry, "支付成功", Toast.LENGTH_SHORT).show()
                        finish()





                    } else {
                        //todo 放弃支付
                        Log.e("支付步骤","3")
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        Toast.makeText(this@HuiyuanzhongxingActiviry, "支付失败", Toast.LENGTH_SHORT).show()
                    }
                }

                SDK_AUTH_FLAG -> {
                    Log.e("支付步骤","4")
                    val authResult: AuthResult = AuthResult(msg.obj as Map<String?, String?>, true)
                    val resultStatus: String = authResult.getResultStatus()

                    // 判断resultStatus 为“9000”且result_code
                    // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档
                    if (TextUtils.equals(
                            resultStatus,
                            "9000"
                        ) && TextUtils.equals(authResult.getResultCode(), "200")
                    ) {
                        Log.e("支付步骤","5")
                        // 获取alipay_open_id，调支付时作为参数extern_token 的value
                        // 传入，则支付账户为该授权账户
                        Toast.makeText(this@HuiyuanzhongxingActiviry, "支付成功", Toast.LENGTH_SHORT).show()
                    } else {
                        Log.e("支付步骤","6")
                        // 其他状态值则为授权失败
                        Toast.makeText(this@HuiyuanzhongxingActiviry, "支付失败", Toast.LENGTH_SHORT).show()
                    }
                }

                else -> { Log.e("支付步骤","7")}
            }
        }
    }
}